package com.itheima.tlias84.servrce.impl;

import com.alibaba.fastjson2.JSONObject;
import com.itheima.tlias84.entity.Emp;
import com.itheima.tlias84.mapper.EmpMapper;
import com.itheima.tlias84.po.LoginParam;
import com.itheima.tlias84.servrce.LoginService;
import com.itheima.tlias84.utils.JwtUtil;
import com.itheima.tlias84.utils.ResultUtil;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.Objects;

@Service
public class LoginServiceImpl implements LoginService {
@Resource
    EmpMapper empMapper;
    @Override
    public ResultUtil login(LoginParam loginParam) {
        Emp emp = empMapper.selectEmpByUsername(loginParam.getUsername());
        if(Objects.isNull(emp)|| !emp.getPassword().equals(loginParam.getPassword())){
            return ResultUtil.fail("用户登录失败");
        }
        String toekn = JwtUtil.generateToken(emp.getUsername(), emp.getPassword(), 30);
        JSONObject result = new JSONObject();
        result.put("id",emp.getId());
        result.put("uesername",emp.getUsername());
        result.put("name",emp.getName());
        result.put("token",toekn);
        return ResultUtil.success(result);
    }
}
